import 'package:flutter_food_delivery_app_ui/model/food_item.dart';

class CartProvider{
  List<FoodItem> foodItems = <FoodItem> [];

  List<FoodItem> addToList(FoodItem foodItem){
    if(foodItems.length == 0){
      foodItem.incrementQuantity();
      foodItems.add(foodItem);
      return foodItems;
    }
    int index = foodItems.indexOf(foodItem,0);
    if(index < 0) {
      foodItems.add(foodItem);
    }
    foodItem.incrementQuantity();

    return foodItems;
  }

  List<FoodItem> removeFrom(FoodItem foodItem){
    //按照id查找
    FoodItem removeFoodItem = foodItems.firstWhere((item){
      return item.id == foodItem.id;
    });
    foodItems.remove(removeFoodItem);
    foodItem.decrementQuantity();
    return foodItems;
  }
}